<?php
require(__DIR__.'/init.php');

class news_solr {
	
	protected $configNews;

	function __construct() {
		$this->configNews = array(
			'endpoint' => array(
				'localhost' => array(
				    'host' => '192.168.14.163',
				    'port' => 8983,
					'core' => 'news',
				    'path' => '/solr/',
				)
			)
		);	
	}

	function select($keyword, $start, $rows, $filterType = 1, $cateId=0, $isExactly=FALSE) {

		// create a client instance
		$client = new Solarium\Client($this->configNews);

		// get a select query instance
		$query = $client->createSelect();

		// search with keyword	
		$keyword = trim($keyword);
		if(isset($keyword) === true && $keyword !== '') {
            if ($isExactly){
                $query->setQuery('catch_all:"'.$keyword.'"');
            }else{
                $query->setQuery('catch_all:'.$keyword);
            }
            if ($filterType & 2){
                $query->createFilterQuery('title')->setQuery('title:'.$keyword);
            }
            if ($filterType & 4){
                $query->createFilterQuery('shortdesc')->setQuery('short_desc:'.$keyword);
            }
            if ($filterType & 8){
                $query->createFilterQuery('fulldesc')->setQuery('full_desc:'.$keyword);
            }            
		} else {
			$query->setQuery('*:*');
		}

		// search in category
		if($cateId != 0)
			$query->createFilterQuery('categoryid')->setQuery('cate_id:'.$cateId);        
        
		// set start and rows param (comparable to SQL limit) using fluent interface
        $query->setStart($start)->setRows($rows);

		// this executes the query and returns the result
		$resultset = $client->select($query);
                  
        return array($resultset->getNumFound(), $resultset);       		
	}

	function insert($newsId, $cateId, $cateName, $title, $shortDesc, $fullDesc, $img, $imgThumb, $imgThumb2, $imgThumb3,
		$url, $parentCateUrl, $tag, $createUser, $createDate, $onlineDate, $displayDate, $isHot, $viewsCount, $status) {
		
		// create a client instance
		$client = new Solarium\Client($this->configNews);

		// get an update query instance
		$update = $client->createUpdate();

		$createDate = date("Y-m-d\TG:i:s\Z", strtotime($createDate));
		$onlineDate = date("Y-m-d\TG:i:s\Z", strtotime($onlineDate));
		$displayDate = date("Y-m-d\TG:i:s\Z", strtotime($displayDate));

		// create a new document for the data
		$doc = $update->createDocument();
		$doc->news_id = $newsId;
		$doc->cate_id = $cateId;
		$doc->cate_name = $cateName;
		$doc->title = $title;
		$doc->short_desc = $shortDesc;
		$doc->full_desc = $fullDesc;
		$doc->img = $img;
		$doc->img_thumb = $imgThumb;
		$doc->img_thumb2 = $imgThumb2;
		$doc->img_thumb3 = $imgThumb3;
		$doc->url = $url;
		$doc->parent_cate_url = $parentCateUrl;
		$doc->tag = $tag;
		$doc->create_user = $createUser;
		$doc->create_date = $createDate;
		$doc->online_date = $onlineDate;
		$doc->display_date = $displayDate;
		$doc->is_hot = $isHot;
		$doc->views_count = $viewsCount;
		$doc->status = $status;

		// add the document and a commit command to the update query
		$update->addDocument($doc);
		$update->addCommit();

		// this executes the query and returns the result
		$result = $client->update($update);

		echo '<b>Query executed</b><br/>';
		echo 'Query status: ' . $result->getStatus(). '<br/>';
		echo 'Query time: ' . $result->getQueryTime();
	
	}

	function update($newsId, $cateId, $cateName, $title, $shortDesc, $fullDesc, $img, $imgThumb, $imgThumb2, $imgThumb3,
		$url, $parentCateUrl, $tag, $createUser, $createDate, $onlineDate, $displayDate, $isHot, $viewCount, $status) {
		return insert($newsId, $cateId, $cateName, $title, $shortDesc, $fullDesc, $img, $imgThumb, $imgThumb2, $imgThumb3,
			$url, $parentCateUrl, $tag, $createUser, $createDate, $onlineDate, $displayDate, $isHot, $viewCount, $status);
	}
}

?>
